cc.Class({
    extends: cc.Component,
    properties: {
        account:null,
	    userId:null,
		userName:null,
		lv:0,
		exp:0,
		coins:0,
		gems:0,
		sign:0,
        ip:"",
        sex:0,
        roomData:null,
        roomData_reconnect:null,
        oldRoomId:null,
        login_id:null,
        img_url:null,
    },
    
    guestAuth:function(){
        // console.log("guestAuth");
        // var account = cc.args["account"];
        // console.log("account="+account);
        // if(account == null){
        //     account = cc.sys.localStorage.getItem("account");
        //     // var sign = cc.sys.localStorage.getItem("sign");
        //     // console.log("------------------account="+account);
        //     // console.log("------------------sign="+sign);
        //     // this.account=account;
        //     // this.sign=sign;
        //     // this.login();
        //     // return;
        // }
        
        // if(account == null){
        //     account = Date.now();
        //     // console.log("-----------------data.now() of account="+account);
        //     cc.sys.localStorage.setItem("account",account);
        //     // console.log("localStorage of account="+account);
        // }
        
        // cc.vv.http.sendRequest("/guest",{account:account},this.onAuth);
        console.log("guestAuth");
        var account = cc.args["account"];
        if(account == null){
            account = cc.sys.localStorage.getItem("account");
        }
        
        if(account == null){
            account = Date.now();
            cc.sys.localStorage.setItem("account",account);
        }
        
        cc.vv.http.sendRequest("/guest",{account:account},this.onAuth);
    },
    
    
    onAuth:function(ret){
        var self = cc.vv.userMgr;
        if(ret.errcode !== 0){
            console.log(ret.errmsg);
        }
        else{
            console.log("cc.vv.si="+cc.vv.SI);
            self.account = ret.account;
            self.sign = ret.sign;
            cc.sys.localStorage.setItem("account",ret.account);
            cc.sys.localStorage.setItem("sign",ret.sign);
            console.log("account="+cc.sys.localStorage.getItem("account")+" sign"+cc.sys.localStorage.getItem("sign"));
            cc.vv.http.url = "http://" + cc.vv.SI.hall;
            console.log("cc.vv.http.url"+cc.vv.http.url);
            self.login();
        }   
    },
    
    login:function(){
        var self = this;
        var onLogin = function(ret){

            if(ret.errcode !== 0){
                console.log(ret.errmsg);
                // cc.vv.login_alert.show("提示","请重新登陆. errcode!=0");
                cc.vv.login_alert.show("提示","登陆失败，请重新登陆！");
                cc.sys.localStorage.removeItem("wx_account");
                cc.sys.localStorage.removeItem("wx_sign");

                cc.sys.localStorage.removeItem("account");
                cc.sys.localStorage.removeItem("sign");
                cc.vv.wc.hide();
            }
            else{
                if(!ret.userid){
                    //jump to register user info.
                    cc.log("jump to register user info");
                    cc.sys.localStorage.removeItem("wx_account");
                    cc.sys.localStorage.removeItem("wx_sign");
                    cc.sys.localStorage.removeItem("account");
                    cc.sys.localStorage.removeItem("sign");    
                    // cc.vv.login_alert.show("提示","请重新登陆(userid=null)");
                    cc.vv.login_alert.show("提示","微信授权失败，请重新启动游戏后登陆");
                    cc.vv.wc.hide();
                }
                else{
                    console.log("login of ====================================");
                    console.log("--------------------------------------");
                    console.log(ret);
                    self.account = ret.account;
        			self.userId = ret.userid;
                    console.log("userId="+self.userId);
        			self.userName = ret.name;
        			self.lv = ret.lv;
        			self.exp = ret.exp;
        			self.coins = ret.coins;
        			self.gems = ret.gems;
                    self.roomData = ret.room;
                    // cc.log("self.roomData",self.roomData);
                    self.roomData_reconnect=ret.room;
                    cc.log("roomData----------------",self.roomData_reconnect);
                    self.login_id=ret.userid;
                    self.img_url=ret.headimg + ".jpg";
                    console.log("self.login_id="+self.login_id);
                    console.log("self.img_url="+self.img_url);
                    self.sex = ret.sex;
                    self.ip = ret.ip;
                    var str="self.account="+self.account+" self.userId="+self.userId;
                    str+=" self.userName="+self.userName+" ";
                    console.log(str);
        			cc.director.loadScene("hall");
                }
            }
        };
        cc.vv.wc.show("正在登录游戏");
        cc.vv.http.sendRequest("/login",{account:this.account,sign:this.sign},onLogin);
    },

    reGetAuth:function(){
        if(!cc.sys.isNative){
            if(account == null){
                account = cc.sys.localStorage.getItem("account");
            }

           var onAuth= function(ret){
                var self = cc.vv.userMgr;
                if(ret.errcode !== 0){
                    console.log(ret.errmsg);
                }
                else{
                    console.log("cc.vv.si="+cc.vv.SI);
                    account = ret.account;
                    sign = ret.sign;
                }   
            }
            cc.vv.http.sendRequest("/guest",{account:account},onAuth);

        }
    },

    reEnterRoom:function(callback){
        console.log("reEnterRoom---------------------------------------------------------------------");
        var account=cc.sys.localStorage.getItem("account");
        var sign=cc.sys.localStorage.getItem("sign");
        if(cc.sys.isNative){
            console.log("enter native ======================================");
            account= cc.sys.localStorage.getItem("wx_account");
            sign= cc.sys.localStorage.getItem("wx_sign");
        }
        
        if(!account||!sign){
            console.log("reEnterRoom was failed bacause account or sign is null");
            return callback(null);
        }

        var onLogin = function(ret){
            if(ret.errcode !== 0){
                console.log("reEnterRoom was failed bacause errcode not equal 0");
                return callback(null);
            }
            if(!ret.userid){
                console.log("reEnterRoom was failed bacause userid is null");
                return callback(null);
            }
            console.log("reEnterRoom /it have data");
            return callback(ret.room);
        }

        cc.vv.http.sendRequest("/login",{account:this.account,sign:this.sign},onLogin);
    },
    
    create:function(name){
        var self = this;
        var onCreate = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                self.login();
            }
        };
        
        var data = {
            account:this.account,
            sign:this.sign,
            name:name
        };
        cc.vv.http.sendRequest("/create_user",data,onCreate);    
    },
    
    enterRoom:function(roomId,callback){
        var self = this;
        var onEnter = function(ret){
            if(callback != null){
                callback(ret);
            }
            if(ret.errcode !== 0){
                cc.vv.wc.hide();
            }
            else{
                console.log("connectGameServer of enterRoom of userMgr.js");
                cc.vv.userMgr.roomData_reconnect=ret;
                cc.vv.userMgr.roomData=ret;
                cc.log("ret-----------------------------",ret);
                cc.vv.gameNetMgr.connectGameServer(ret);
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            roomid:roomId
        };
        cc.vv.wc.show("正在进入房间");
        cc.vv.http.sendRequest("/enter_private_room",data,onEnter);
    },
    getHistoryList:function(callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                // console.log(ret.history);
                if(callback != null){
                    callback(ret.history);
                }
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
        };
        cc.vv.http.sendRequest("/get_history_list",data,onGet);
    },
    getGamesOfRoom:function(uuid,callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log(ret.data);
                callback(ret.data);
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid,
        };
        cc.vv.http.sendRequest("/get_games_of_room",data,onGet);
    },
    // Hello:function(callback){
    //     // callback(100); 
    //     // callback(null);
    // },
    
    getDetailOfGame:function(uuid,index,callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log(ret.data);
                callback(ret.data);
            }       
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid,
            index:index,
        };
        cc.vv.http.sendRequest("/get_detail_of_game",data,onGet);
    }
});
